import { setStore, getStore } from '@/utils/store'
import { loginByUsername } from '@/api/login/login'
import { setToken, removeToken } from '@/utils/auth'
import { getCurrentUserInfo } from '@/api/sys/user'
const user = {
  state: {
    access_token: getStore({ name: 'access_token' }) || '',
    user: getStore({ name: 'user' }) || {},
    roleList: getStore({ name: 'roleList' }) || [],
    deptList: getStore({ name: 'deptList' }) || [],
    permissions: getStore({ name: 'permissions' }) || {},
    isGuide: getStore({ name: 'isGuide' }) || false,
    systemList: getStore({ name: 'systemList' }) || []
  },
  actions: {
    LoginByUsername ({ commit, state, dispatch }, userInfo) {
      return new Promise((resolve, reject) => {
        loginByUsername(userInfo.username, userInfo.password, userInfo.code, userInfo.randomStr).then(data=>{
          //console.log(data.access_token,data,data.data,data.data.access_token)
          setToken(data.access_token)
          commit('SET_ACCESS_TOKEN', data.access_token)
          commit('SET_REFRESH_TOKEN', data.refresh_token)
          // 登陆成功后，获取用户信息放到store中
          getCurrentUserInfo().then(data=>{
            console.log(data,"info=====");
          }).catch(error=>{
            reject(error)
          })
          resolve()
        }).catch(error=>{
          reject(error)
        })
      })
    }
  },
  mutations: {
    /**
     * 保存access_token
     * @param state
     * @param accessToken
     * @constructor
     */
    SET_ACCESS_TOKEN: (state, accessToken) => {
      state.access_token = accessToken
      setStore({
        name: 'access_token',
        content: state.access_token,
        type: 'session'
      })
    },
  }
}

export default user
